package org.snaker.engine;

import java.util.Collections;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snaker.engine.access.dialect.Dialect;
import org.snaker.engine.access.dialect.H2Dialect;
import org.snaker.engine.access.dialect.MySqlDialect;
import org.snaker.engine.access.dialect.OracleDialect;
import org.snaker.engine.access.dialect.PostgresqlDialect;
import org.snaker.engine.access.dialect.SQLServerDialect;
import org.snaker.engine.core.ServiceContext;
import org.snaker.engine.entity.Process;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.dialect.DialectFactory;
import cn.hutool.json.JSONUtil;

/**
 * 工具类
 */
public final class TgSnakerUitl {
    private static final Logger log = LoggerFactory.getLogger(TgSnakerUitl.class);

    private TgSnakerUitl() {
        throw new Error("工具类不能实例化！");
    }

    /**
     * 根据连接对象获取数据库方言
     * 
     * @param conn
     *            数据库连接
     * @return Dialect 方言
     * @throws Exception
     */
    public static Dialect getDialect(DataSource ds) throws Exception {
        String dbType = DialectFactory.getDialect(ds).dialectName();
        if (StrUtil.isBlank(dbType))
            return null;
        if (dbType.equalsIgnoreCase("mysql"))
            return new MySqlDialect();
        else if (dbType.equalsIgnoreCase("oracle"))
            return new OracleDialect();
        else if (dbType.equalsIgnoreCase("postgres"))
            return new PostgresqlDialect();
        else if (dbType.equalsIgnoreCase("mssql"))
            return new SQLServerDialect();
        else if (dbType.equalsIgnoreCase("h2"))
            return new H2Dialect();
        else
            return null;
    }

    /**
     * 判断是否已经执行过脚本[暂时根据wf_process表是否有数据]
     * 
     * @return 是否执行成功
     */
    public static boolean isExec() {
        try {
            Db.use().execute("select * from wf_process");
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * getVariableMap
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> getVariableMap(final String variable) {
        Map<String, Object> map = Collections.emptyMap();
        if (StrUtil.isNotBlank(variable)) {
            map = JSONUtil.toBean(variable, Map.class);
        }
        return map;
    }

    /**
     * getProcessName
     */
    public static String getProcessName(final String processId) {
        SnakerEngine engine = ServiceContext.getEngine();
        Process process = engine.process().getProcessById(processId);
        if (process == null) {
            return processId;
        }
        return process.getDisplayName();
    }

    /**
     * 将对象转换为json字符串
     * 
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        if (object == null)
            return "";
        try {
            return JSONUtil.toJsonStr(object);
        } catch (Exception e) {
            log.warn("write to json string error:" + object, e);
            return "";
        }
    }
}
